/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousPostMapping;
import me.zhengjie.modules.anniversary.domain.AssociationUserInfo;
import me.zhengjie.modules.anniversary.domain.Photos;
import me.zhengjie.modules.anniversary.service.AssociationUserInfoService;
import me.zhengjie.modules.anniversary.service.dto.AssociationUserInfoQueryCriteria;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "校友会人员管理")
@RequestMapping("/api/associationUserInfo")
public class AssociationUserInfoController {

    private final AssociationUserInfoService associationUserInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('associationUserInfo:list')")
    public void download(HttpServletResponse response, AssociationUserInfoQueryCriteria criteria) throws IOException {
        associationUserInfoService.download(associationUserInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询校友会人员")
    @ApiOperation("查询校友会人员")
    //@PreAuthorize("@el.check('associationUserInfo:list')")
    @AnonymousAccess
    public ResponseEntity<Object> query(AssociationUserInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(associationUserInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    //根据id查询校友会
    @Log("查询校友会人员")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/query")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getAssociationsByUserId(Long userId,Pageable pageable) {
        List<Map<String,Object>> associations = associationUserInfoService.getAssociationsByUserId(userId, pageable);
        System.out.println(associations);
        return new ResponseEntity<>(associations,HttpStatus.OK);
    }


    //根据id和type查询校友会
    @Log("查询校友会人员")
    @ApiOperation("查询校友通讯录:根据ID获取同级与上级数据")
    @GetMapping("/queryType")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getAssociationsByType(Long userId,Integer type, Pageable pageable) {
        List<Map<String,Object>> associations = associationUserInfoService.getAssociationsByType(userId, type, pageable);
        System.out.println(associations);
        return new ResponseEntity<>(associations,HttpStatus.OK);
    }

    @Log("新增校友会人员")
    @ApiOperation("新增校友会人员")
    @PreAuthorize("@el.check('associationUserInfo:add')")
    public ResponseEntity<Object> create(AssociationUserInfo resources){
        return new ResponseEntity<>(associationUserInfoService.create(resources),HttpStatus.CREATED);
    }

    @Log("新增校友会人员")
    @ApiOperation("新增校友会人员")
    @AnonymousPostMapping(value = "/addUserInfo")
    public ResponseEntity<Object> addUserInfo(@RequestBody AssociationUserInfo resources){
        resources.setIsEnable(true);
        return new ResponseEntity<>(associationUserInfoService.create(resources),HttpStatus.CREATED);
    }

    @Log("根据校友会id和userid查询成员")
    @ApiOperation("根据校友会id和userid查询成员")
//    @AnonymousPostMapping(value = "/queryUserInfo")
    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
    public ResponseEntity<Object> queryUserInfo(@RequestBody Map<String, Long> map){
        return new ResponseEntity<>(associationUserInfoService.queryByIdAndUserId(map.get("assId"), map.get("userId")),HttpStatus.CREATED);
    }

    @Log("根据校友会id删除成员")
    @ApiOperation("根据校友会id删除成员")
    @AnonymousPostMapping(value = "/deleteUserInfo")
    public ResponseEntity<Object> deleteUserInfo(@RequestBody AssociationUserInfo resources){
        associationUserInfoService.deleteUserInfoById(resources.getId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @Log("修改校友会人员")
    @ApiOperation("修改校友会人员")
    @PreAuthorize("@el.check('associationUserInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AssociationUserInfo resources){
        associationUserInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除校友会人员")
    @ApiOperation("删除校友会人员")
    @PreAuthorize("@el.check('associationUserInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        associationUserInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("通过姓名查找校友会人员~小程序端")
    @ApiOperation("通过姓名查找校友会人员~小程序端")
    //@PreAuthorize("@el.check('associationUserInfo:del')")
    @AnonymousAccess
    @GetMapping(value = "uniapp",produces = "text/html;charset=utf-8")
    @ResponseBody
    public ResponseEntity<Object> queryByName(@RequestParam("name") String name,@RequestParam("page") Integer page,@RequestParam("size") Integer size) {
        return new ResponseEntity<>(JSON.toJSONString(associationUserInfoService.queryByName(name, PageRequest.of(page, size))),HttpStatus.OK);
    }

    @Log("通过userID查找校友会人员~小程序端")
    @ApiOperation("通过userID查找校友会人员~小程序端")
    //@PreAuthorize("@el.check('associationUserInfo:del')")
    @AnonymousAccess
    @GetMapping(value = "queryByUserId",produces = "text/html;charset=utf-8")
    @ResponseBody
    public ResponseEntity<Object> queryByUserId(@RequestParam("userId") String userId, @RequestParam("wxNickName") String wxNickName) {
        List<Map<String,Object>> mapList = associationUserInfoService.queryByUserId(Long.parseLong(userId), Long.parseLong(wxNickName));
        return new ResponseEntity<>(JSON.toJSONString(mapList), HttpStatus.OK);
    }

    @Log("查询校友会成员教师信息详情")
    @ApiOperation("查询校友会成员教师信息详情")
    //@PreAuthorize("@el.check('associationUserInfo:list')")
    @GetMapping(value = "/associationUserTeaDetail",produces = "text/html;charset=utf-8")
    @ResponseBody
    @AnonymousAccess
    public ResponseEntity<Object> queryByUseridTeaDetail(@RequestParam Long id){
        return new ResponseEntity<>(JSON.toJSONString(associationUserInfoService.queryByUseridTeaDetail(id)), HttpStatus.OK);
    }

    @Log("查询校友会成员学生信息详情")
    @ApiOperation("查询校友会成员学生信息详情")
    //@PreAuthorize("@el.check('associationUserInfo:list')")
    @GetMapping(value = "/associationUserStuDetail",produces = "text/html;charset=utf-8")
    @ResponseBody
    @AnonymousAccess
    public ResponseEntity<Object> queryByUseridStuDetail(@RequestParam Long id){
        return new ResponseEntity<>(JSON.toJSONString(associationUserInfoService.queryByUseridStuDetail(id)), HttpStatus.OK);
    }

    @Log("通过校友会id查询成员")
    @ApiOperation("通过校友会id查询成员")
    //@PreAuthorize("@el.check('associationUserInfo:list')")
    @AnonymousAccess
    @GetMapping(value = "/associationUser",produces = "text/html;charset=utf-8")
    @ResponseBody
    public ResponseEntity<Object> queryByAssociationId(@RequestParam Long id,@RequestParam(value="userName",required=false,defaultValue="")String userName,@RequestParam Integer page,@RequestParam Integer size) {
        return new ResponseEntity<>(JSON.toJSONString(associationUserInfoService.queryByAssociationId(id,userName,PageRequest.of(page,size))), HttpStatus.OK);
    }

    @Log("通过校友会id查询照片")
    @ApiOperation("通过校友会id查询照片")
    //@PreAuthorize("@el.check('associationUserInfo:list')")
    @AnonymousAccess
    @GetMapping(value = "/queryPhotoByAssId",produces = "text/html;charset=utf-8")
    @ResponseBody
    public ResponseEntity<Object> queryPhotoByAssId(@RequestParam Long id) {
        return new ResponseEntity<>(JSON.toJSONString(associationUserInfoService.queryPhotoByAssId(id)), HttpStatus.OK);
    }

    @Log("校友会上传图片")
    @ApiOperation("将信息保存到数据库中")
    @AnonymousPostMapping(value = "/photo")
    public String photo(@RequestBody JSONObject parm){
        try {
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            Photos photos = new Photos();
            photos.setName("校友会相册"+parm.get("id").toString());
            photos.setUserId(Long.parseLong(parm.get("userId").toString()));
            photos.setAssId(Long.parseLong(parm.get("id").toString()));
            photos.setType("0");
            photos.setTime(ts);
            photos.setPath(parm.get("photoUrl").toString());
            photos.setVerifyStatus(0);
            photos.setCreateBy(parm.get("username").toString());
            photos.setCreateTime(ts);
            photos.setIsEnable(true);
            int i = associationUserInfoService.insertPhotos(photos);
            return String.valueOf(i);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "0";
    }
}
